const app = getApp();
const api = require('../../utils/interface.js');
const network = require("../../utils/network.js")
Page({
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    code: '',
    MemberAgreementState: false,
    xiyiState: true,
    state: "",
    form: null,
    options: null,
    bok:true,
    params:""
  },
  // 跳转密码登陆
  login() {
    if (!this.data.xiyiState) {
      this.setData({
        MemberAgreementState: true
      })
      return false
    }
    wx.navigateTo({
      url: '/pages/login/adminUser/index?state=' + JSON.stringify(this.data.options),
    })
  },
  onLoad: function (options) {
    let that = this
    if(options.page){
      this.data.options = options.page
      this.data.state = options.state
      this.data.params = options
    } 
    wx.login({
      success: function (res) {
        that.data.code = res.code
      },
      fail: function (err) {
      }
    })
  },
  // 授权协议弹窗
  logina() {
    if (!this.data.xiyiState) {
      this.setData({
        MemberAgreementState: true
      })
      return false
    }
  },
  bindGetUserInfo: function (e) {

    // 未处理逻辑 拒绝授权手机号登陆
    var that = this;
    if(!this.data.bok){
      return false
    }
    this.data.bok = false
    let data = {
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
      code: that.data.code
    }
    api.getUserPhone(data).then(res => {
      if (res.data.code == '200') {
        let data2 = {
          phone: res.data.data.phoneNumber,
          openid: res.data.data.openId,
        }
        api.getLogin(data2).then(res => {
          if (res.data.code == 200) {
            wx.setStorageSync('userInfo', res.data.data)
            wx.setStorageSync('jwt', res.data.data.jwt)
            console.log(that.searchUrl(that.data.params))
            if (that.data.state == 1) {
              wx.reLaunch({
                url: that.searchUrl(that.data.params)
              })
            } else {
              wx.switchTab({
                url: that.searchUrl(that.data.params)
              })
            }
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false
            })
          }
        })

      } else {
        that.data.bok = true
      }
    }).catch(e => {
      that.data.bok = true
      console.log(e)
    });
    
  },
  xiyi() {
    this.setData({
      MemberAgreementState: true
    })
  },
  select() {
    this.setData({
      xiyiState: !this.data.xiyiState
    })
  },
  gh(e) {
    this.setData({
      xiyiState: true
    })
  },
  searchUrl(options,page){
    console.log(options)
    let params = '/'+options.page+'?'
  
    for (var i in options) {
      if(i!='page'){
        params += (i + '=' + options[i]+'&')
      }
     
    }
    params = params.substring(0, params.length - 1);  
    console.log(params)
    return params
  }
})